package com.hongsen.cn.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentTransaction;

import com.hongsen.cn.Utils.RxBus;
import com.hongsen.cn.application.DayAndMonthApplication;
import com.hongsen.cn.base.BaseActivity;
import com.hongsen.cn.bean.RxValue;
import com.hongsen.cn.bean.TheKeyPersonelBean;
import com.hongsen.cn.contract.MainView;
import com.hongsen.cn.dayandmonth.R;
import com.hongsen.cn.dayandmonth.databinding.ActivityMainBinding;
import com.hongsen.cn.fragment.AddToolFragment;
import com.hongsen.cn.fragment.DynamicFragment;
import com.hongsen.cn.fragment.ForAShortTimeFragment;
import com.hongsen.cn.fragment.GetOutOfLineFragment;
import com.hongsen.cn.fragment.HomeFragment;
import com.hongsen.cn.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

import static com.hongsen.cn.bean.RxBusEventKey.BASE_TITLE_CLICK;
import static com.hongsen.cn.bean.RxBusEventKey.HIDE_FRAGMENT_AND_SHOW;

public class MainActivity extends BaseActivity<MainPresenter> implements MainView, View.OnClickListener {

    private List<TheKeyPersonelBean> listOutTimes = new ArrayList<>();//加戴械具超时
    public ActivityMainBinding binding;
    public HomeFragment homeFragment;


    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(MainActivity.this);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (DayAndMonthApplication.getInstance().isLockFlag()) {
            startActivity(new Intent(getBaseContext(), UnClockActivity.class));
        }
    }


    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        View inflate = View.inflate(this, R.layout.activity_main, null);
        baseFl.addView(inflate);

        binding = DataBindingUtil.inflate(LayoutInflater.from(MainActivity.this), R.layout.activity_main, null, false);
        binding.setActivity(this);

        //默认显示homeFragment
        initFragment();

        //baseActivity的点击事件
        RxBus.getDefault()
                .toObservable(RxValue.class)
                .observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Consumer<RxValue>() {
                    @Override
                    public void accept(RxValue rxValue) throws Exception {
                        if (rxValue.getKey().equals(BASE_TITLE_CLICK)) {
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            hideFragment(ft);
                            String value = rxValue.getValue();
                            if (value.equals("动态人员")) {
                                if (dynamicFragment == null) {
                                    dynamicFragment = new DynamicFragment();
                                    ft.add(R.id.base_fl, dynamicFragment, DynamicFragment.class.getName());
                                } else {
                                    ft.show(dynamicFragment);
                                }
                            } else if (value.equals("加戴械具")) {
                                if (addToolFragment == null) {
                                    addToolFragment = new AddToolFragment();
                                    ft.add(R.id.base_fl, addToolFragment, AddToolFragment.class.getName());
                                } else {
                                    ft.show(addToolFragment);
                                }
                            } else if (value.equals("今日违规")) {
                                if (getOutOfLineFragment == null) {
                                    getOutOfLineFragment = new GetOutOfLineFragment();
                                    ft.add(R.id.base_fl, getOutOfLineFragment, GetOutOfLineFragment.class.getName());
                                } else {
                                    ft.show(getOutOfLineFragment);
                                }
                            } else if (value.equals("出所人员")) {
                                if (forAShortTimeFragment == null) {
                                    forAShortTimeFragment = new ForAShortTimeFragment();
                                    ft.add(R.id.base_fl, forAShortTimeFragment, ForAShortTimeFragment.class.getName());
                                } else {
                                    ft.show(forAShortTimeFragment);
                                }
                            }
                            ft.commit();

                        } else if (rxValue.getKey().equals(HIDE_FRAGMENT_AND_SHOW)) {
                            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                            hideFragment(ft);
                            if (homeFragment == null) {
                                homeFragment = new HomeFragment();
                                ft.add(R.id.flModule, homeFragment, HomeFragment.class.getName());
                            } else {
                                ft.show(homeFragment);
                            }
                            ft.commit();
                        }
                    }
                });

    }

    public void initFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideFragment(ft);
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
            ft.add(R.id.flModule, homeFragment, HomeFragment.class.getName());
        } else {
            ft.show(homeFragment);
        }
        ft.commit();
    }

    public void hideFragment(FragmentTransaction ft) {
        if (getOutOfLineFragment != null) {
            ft.hide(getOutOfLineFragment);
        }

        if (addToolFragment != null) {
            ft.hide(addToolFragment);
        }

        if (dynamicFragment != null) {
            ft.hide(dynamicFragment);
        }

        if (forAShortTimeFragment != null) {
            ft.hide(forAShortTimeFragment);
        }

        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
    }




}